package openvote

import org.springframework.dao.DataIntegrityViolationException

class CandidatoController {
	def beforeInterceptor = [action:this.&checkUser]
	def checkUser() {
		if(!session.usuario) {
		// i.e. user not logged in
		redirect(controller:'usuario',action:'login')
		
		return false
		}
	}
	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index() {
		redirect(action: "list", params: params)
	}

	def list(Integer max) {
		params.max = Math.min(max ?: 10, 100)
		[candidatoInstanceList: Candidato.list(params), candidatoInstanceTotal: Candidato.count()]
	}

	def create() {
		[candidatoInstance: new Candidato(params)]
	}

	def save() {
		def candidatoInstance = new Candidato(params)
		if (!candidatoInstance.save(flush: true)) {
			render(view: "create", model: [candidatoInstance: candidatoInstance])
			return
		}

		flash.message = message(code: 'default.created.message', args: [message(code: 'candidato.label', default: 'Candidato'), candidatoInstance.id])
		redirect(action: "show", id: candidatoInstance.id)
	}

	def show(Long id) {
		def candidatoInstance = Candidato.get(id)
		if (!candidatoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'candidato.label', default: 'Candidato'), id])
			redirect(action: "list")
			return
		}

		[candidatoInstance: candidatoInstance]
	}

	def edit(Long id) {
		def candidatoInstance = Candidato.get(id)
		if (!candidatoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'candidato.label', default: 'Candidato'), id])
			redirect(action: "list")
			return
		}

		[candidatoInstance: candidatoInstance]
	}

	def update(Long id, Long version) {
		def candidatoInstance = Candidato.get(id)
		if (!candidatoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'candidato.label', default: 'Candidato'), id])
			redirect(action: "list")
			return
		}

		if (version != null) {
			if (candidatoInstance.version > version) {
				candidatoInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
						[message(code: 'candidato.label', default: 'Candidato')] as Object[],
						"Another user has updated this Candidato while you were editing")
				render(view: "edit", model: [candidatoInstance: candidatoInstance])
				return
			}
		}

		candidatoInstance.properties = params

		if (!candidatoInstance.save(flush: true)) {
			render(view: "edit", model: [candidatoInstance: candidatoInstance])
			return
		}

		flash.message = message(code: 'default.updated.message', args: [message(code: 'candidato.label', default: 'Candidato'), candidatoInstance.id])
		redirect(action: "show", id: candidatoInstance.id)
	}

	def delete(Long id) {
		def candidatoInstance = Candidato.get(id)
		if (!candidatoInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'candidato.label', default: 'Candidato'), id])
			redirect(action: "list")
			return
		}

		try {
			candidatoInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'candidato.label', default: 'Candidato'), id])
			redirect(action: "list")
		}
		catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'candidato.label', default: 'Candidato'), id])
			redirect(action: "show", id: id)
		}
	}



	def pesquisa() {

		try {

			def query = Candidato.where {


				if (!((String)(params.nome).trim()).equals("")) {
					(nome == ((String)(params.nome).trim()))
				}
				
				if (!((String)(params.numero).trim()).equals("")) {
					(numero == ((String)(params.numero).trim()))
				}

				if (!((String)(params.partido).trim()).equals("")) {
					partido == ((String)(params.partido).trim())
				}
			}

			if (((String)(params.nome).trim()).equals("") && ((String)(params.numero).trim()).equals("") && ((String)(params.partido).trim()).equals("")) {
				[candidatoInstanceList: new Candidato(),candidatoInstanceTotal: Candidato.count()]
			}

			else {
				[candidatoInstanceList: query.list(),candidatoInstanceTotal: Candidato.count()]
			}
		}
		catch (NullPointerException e) {
			[candidatoInstanceList: new Candidato(),candidatoInstanceTotal: Candidato.count()]
		}
	}

}
